<template>
  <!--<el-card class="empty">
    <div class="card-list">
      <div class="v-card" v-for="(item, index) in videoList" :key="index">
        <el-card class="video-card">
          <div v-if="item.img" @click="selectItem(item)">
            <el-image :src="item.img | coverUrl" lazy class="pic"></el-image>
          </div>
          <div v-else>
            <el-image :src="null" class="pic"></el-image>
          </div>
          <div class="title">
            <span v-if="item.title">{{item.title}}</span>
            <span v-else>视频不见了</span>
          </div>
          <div class="videoData">
            <el-button type="text" @click="deleteVideo(item)">
              <i class="el-icon-delete"></i>删除
            </el-button>
          </div>
        </el-card>
      </div>
    </div>
    <div style="text-align:center;margin-top:30px">
      <el-pagination
        background
        layout="prev, pager, next"
        :current-page="page"
        :total="count"
        :page-size="page_size"
        :hide-on-single-page="shouPagination"
        @current-change="handleCurrent"
      ></el-pagination>
    </div>
  </el-card>-->
  <el-card class="empty">
    <div class="card-list">
      <div class="m-card" v-for="(item, index) in videoList" :key="index">
        <div class="card" @click="selectItem(item.vid)">
          <el-image class="m-cover" :src="item.img | coverUrl" lazy></el-image>
          <div class="count">
            <p class="title">{{ item.title }}</p>
          </div>
        </div>
        <div>
          <div style="float:right">
            <el-button icon="el-icon-delete" type="text"  @click="deleteVideo(item,index)">取消收藏</el-button>
          </div>
        </div>
      </div>
    </div>
  </el-card>
</template>

<script>
import { getFavorites, unfavorites } from "@/api/video";
import { coverUrl } from "@/utils/url";
export default {
  data() {
    return {
      videoList: [],
      index:'',
      page: 0,
      page_size: 6,
      count: 0,
      shouPagination: true, //隐藏分页
      formId: {
        id: "",
      },
    };
  },
  methods: {
    _getVideoList() {
      getFavorites(this.page, this.page_size).then((res) => {
        if (res.data.code === 200) {
          if (this.count == 0) {
            //只有查询第一页才返回总数
            this.count = res.data.data.count;
            if (this.count > this.page_size) {
              this.shouPagination = false;
            }
          }
          this.videoList = res.data.data.data;
        }
      });
    },
    selectItem(item) {
      this.$router.push({ name: "video", params: { vid: item.vid } });
      //跳转视频页面
    },
    handleCurrent(page) {
      this.page = page;
      this._getVideoList();
    },
    deleteVideo(item,index) {
      this.index = index
      this.formId.id = Number(item.vid);
      unfavorites(this.formId).then((res) => {
        if (res.data.code === 200) {
          this.$message.success("取消收藏");
          this.videoList.splice(this.index, 1);
        }
      }).catch((err) => {
        this.$message.error(err.response.data.msg);
      });
    },
  },
  created() {
    this.page = 1;
    this._getVideoList();
  },
  filters: {
    coverUrl(name) {
      return coverUrl(name);
    },
  },
};
</script>
<style >
.empty {
  min-height: 500px;
}
.card-list {
  display: flex;
  flex-wrap: wrap;
}

.m-card {
  display: inline-block;
  width: 25%;
  height: 240px;
  padding: 8px 5px;
  box-sizing: border-box;
}

.card {
  position: relative;
  overflow: hidden;
  border-radius: 3px;
}

.m-cover {
  top: 0;
  left: 0;
  width: 100%;
  height: 180px;
}

.count {
  display: flex;
  justify-content: space-between;
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  padding: 5px 6px;
  height: 80px;
  box-sizing: border-box;
  color: #ffffff;
  background: linear-gradient(0deg, rgba(0, 0, 0, 0.85), transparent);
}

.title {
  font-size: 13px;
  line-height: 16px;
  margin-top: 40px;
  color: #fff;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
}
</style>


